<?php
include_once 'conf/DBD.php';

class GDB extends DbData{
	/*****
	 * Datenbank aktuelle connect
	 *****/
	protected $Con;

	/*****
	 * Letzte Sql-Anweissung
	 *****/
	private $GDBsql;

	/*****
	 * Das letzte Ergebniss
	 *****/
	protected $result;

	public $error = false;
	public $insert_id = null;
	public $errno = null;

	#Der Konstruktor
	function __construct($dns = NULL) { 
	    
	    if($dns)
	    {
	        $dsninfo = DB::parseDSN($dns);
	        $this->DbServer = $dsninfo['hostspec'];
	        $this->DbUser = $dsninfo['username'];
	        $this->DbPassword = $dsninfo['password'];
	        $this->DbTabele = $dsninfo['database'];
	    }
	    
		try{
			$this->connect();
		}catch (Exception $e){
			$e->getMessage();
		}
	}

	#Das Connection auf die Datenbank
	function connect() {
		$this->DbData();
		$this->Con = mysql_connect ( $this->DbServer,$this->DbUser, $this->DbPassword);
		if (mysql_error ())
		throw new Exception ( "Error Code 1: Ups beim Verbinden mit der Datenbank () ist ein fehler unterlaufen" );
		$this->setDbTabele();
	}
	public static function startGDB($scn = NULL) {
		return new GDB($scn);
	}
	#Datenbank Tabele Auswï¿½hlen
	function setDbTabele() {
		$select_db = mysql_select_db ($this->DbTabele, $this->Con );
	    
    	if (!$select_db) {    	    
            die ('Kann foo nicht benutzen : ' . mysql_error());
        }
	}

	#Select Anweisung wird ausgefï¿½hrt
	function Select($sql) {
		$this->GDBsql = $sql;
		$this->result = mysql_query($sql,$this->Con);
		
		if(mysql_error()){		   
		    throw new DbError(mysql_error(),$sql,mysql_errno());
		}
		$this->insert_id = mysql_insert_id();
	}

	public function __sleep() {
		return array ('server', 'username', 'password', 'db' );
	}

	public function __wakeup() {
		$this->connect ();
	}

	function affectedrows() {
		return (@mysql_affected_rows ( $this->Con ));
	}

	function numrows($con) {
		return (@mysql_num_rows ( $con ));
	}

	function fetchobject($count = 0) {
		$final = array();
		
		while ( false != ($lauf = mysql_fetch_object($this->result ))) {
			array_push($final,$lauf);
		}
		
		if($count == 1){
		    if(isset($final[0])){
		        return $final[0];	
		    }	    
		}else{
		    return $final;		    
		}
	}

	function fetcharray($count = 0) {
	    $final = array();

		while ( false !== ($lauf = mysql_fetch_array ( $this->result ))) {
			array_push($final,$lauf);
		}
	    if($count == 1){
		    return $final[0];		    
		}else{
		    return $final;		    
		}
	}

	function fetchassoc($count = 0) {
		$final = array();

		while ( false !== ($lauf = mysql_fetch_assoc ( $this->result ))) {
			array_push($final,$lauf);
		}
	    if($count == 1){
		    return $final[0];		    
		}else{
		    return $final;		    
		}
	}

}

?>